//给定一个 N 叉树，返回其节点值的层序遍历。（即从左到右，逐层遍历）。 
//
// 树的序列化输入是用层序遍历，每组子节点都由 null 值分隔（参见示例）。 
//
// 
//
// 示例 1： 
//
// 
//
// 
//输入：root = [1,null,3,2,4,null,5,6]
//输出：[[1],[3,2,4],[5,6]]
// 
//
// 示例 2： 
//
// 
//
// 
//输入：root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,
//null,13,null,null,14]
//输出：[[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]
// 
//
// 
//
// 提示： 
//
// 
// 树的高度不会超过 1000 
// 树的节点总数在 [0, 10^4] 之间 
// 
// Related Topics 树 广度优先搜索 👍 222 👎 0

package leetcode.editor.cn;

import java.util.*;

public class _429_NAryTreeLevelOrderTraversal {

    public static class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }

    public static void main(String[] args) {
        Solution solution = new _429_NAryTreeLevelOrderTraversal().new Solution();
        List<Node> nodes1 = new ArrayList<>();
        nodes1.add(new Node(2));
            List<Node> nodes3 = new ArrayList<>();
            nodes3.add(new Node(6));
                List<Node> nodes7 = new ArrayList<>();
                    List<Node> nodes11 = new ArrayList<>();
                    nodes11.add(new Node(14));
                nodes7.add(new Node(11, nodes11));
            nodes3.add(new Node(7, nodes7));
        nodes1.add(new Node(3, nodes3));
            List<Node> nodes4 = new ArrayList<>();
                List<Node> nodes8 = new ArrayList<>();
                nodes8.add(new Node(12));
            nodes4.add(new Node(8, nodes8));
        nodes1.add(new Node(4, nodes4));
            List<Node> nodes5 = new ArrayList<>();
                List<Node> nodes9 = new ArrayList<>();
                nodes9.add(new Node(13));
            nodes5.add(new Node(9, nodes9));
            nodes5.add(new Node(10));
        nodes1.add(new Node(5, nodes5));
        Node root = new Node(1, nodes1);

        System.out.println(solution.levelOrder(root));
    }

    class Solution {
        public List<List<Integer>> levelOrder(Node root) {
            if (root == null) {
                return new ArrayList<List<Integer>>();
            }

            List<List<Integer>> ans = new ArrayList<List<Integer>>();
            Queue<Node> queue = new ArrayDeque<Node>();
            queue.offer(root);

            while (!queue.isEmpty()) {
                int cnt = queue.size();
                List<Integer> level = new ArrayList<Integer>();
                for (int i = 0; i < cnt; ++i) {
                    Node cur = queue.poll();
                    level.add(cur.val);
                    for (Node child : cur.children) {
                        queue.offer(child);
                    }
                }
                ans.add(level);
            }

            return ans;
        }
    }

    //leetcode submit region begin(Prohibit modification and deletion)
/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

    class Solution1 {
        public List<List<Integer>> levelOrder(Node root) {
            if (root == null) return new ArrayList<List<Integer>>();

            List<List<Integer>> res = new ArrayList<>();
            Queue<Node> queue = new ArrayDeque<>();
            queue.add(root);
            int count = 0;
            Node node = null;
            List<Integer> tmp = null;
            while (!queue.isEmpty()) {
                count = queue.size();
                tmp = new ArrayList<>();
                for (int i = 0; i < count; i++) {
                    node = queue.poll();
                    if (node.children != null) {
                        for (Node n : node.children) {
                            queue.offer(n);
                        }
                    }
                    tmp.add(node.val);
                }
                res.add(tmp);
            }
            return res;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}